
package com.wyzz.hr.bo;

import java.util.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.directwebremoting.WebContext;
import org.directwebremoting.WebContextFactory;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.wyzz.hr.dao.IDepartmentDAO;
import com.wyzz.hr.pojo.Department;
import com.wyzz.hr.pojo.Depthist;
import com.wyzz.hr.pojo.Employee;
import com.wyzz.hr.pojo.Position;
import com.wyzz.hr.pojo.PositionBase;
import com.wyzz.hr.pojo.PositionBaseHist;
import com.wyzz.hr.pojo.Userinfo;
import com.wyzz.hr.spring.SpringBeanFactory;
import com.wyzz.hr.util.StringUtil;


public class OrgDeptOperateHelper {

	public OrgDeptOperateHelper() {
		msgDisableDept = "{0}\u88AB\u505C\u7528\u3002";
		msgEnablePB = "{0}\u88AB\u542F\u7528\u3002";
		msgEnableDept = "{0}\u88AB\u542F\u7528\u3002";
		msgRenameDept = "\u90E8\u95E8\u540D\u79F0\u7531{0}\u6539\u4E3A{1}\u3002";
		msgMoveDept = "\u90E8\u95E8{0}\u4ECE{1}\u79FB\u52A8\u5230{2}\u3002";
		msgMovePB = "\u804C\u4F4D{0}\u4ECE{1}\u79FB\u52A8\u5230{2}";
		msgMovePBDept = "\u804C\u4F4D{0}\u4ECE\u90E8\u95E8{1}\u79FB\u52A8\u5230\u90E8\u95E8{2}\u3002";
		msgDepIsUsed = "\u8BE5\u90E8\u95E8(\u53CA\u4E0B\u5C5E\u90E8\u95E8)\u6709{0}\u6709\u4EBA\u5728\u804C\uFF0C\u4E0D\u80FD\u505C\u7528\uFF01";
		msgPBExceed = "\u90E8\u95E8({0})\u4E0B\u7684({1})\u804C\u4F4D\u8D85\u7F16\uFF01";
		msgDisablePBRelate = "\u804C\u4F4D{0}\u4E0E{1}\u6709\u5173\u8054\uFF0C\u4E0D\u80FD\u505C\u7528\uFF01";
		msgDisablePB = "{0}\u88AB\u505C\u7528\u3002";
		msgDelPBExistInEmpOrgHist = "{0}\u5728\u5458\u5DE5\u7EC4\u7EC7\u7ED3\u6784\u5386\u53F2\u4E2D\u5B58\u5728\uFF0C\u4E0D\u80FD\u5220\u9664\uFF01";
		msgDelPBIsUsed = "\u804C\u4F4D{0}\u6709\u4EBA\u5728\u804C\uFF0C\u4E0D\u80FD\u505C\u7528\uFF01";
		msgDeletePBIsUsed = "\u804C\u4F4D{0}\u6709\u4EBA\u5728\u804C\uFF0C\u4E0D\u80FD\u5220\u9664\uFF01";
		msgPBRelate = "\u804C\u4F4D{0}\u4E0E{1}\u6709\u5173\u8054\uFF0C\u4E0D\u80FD\u5220\u9664\uFF01";
		msgDeleteDepIsUsed = "{0}\u90E8\u95E8(\u6216\u4E0B\u5C5E\u90E8\u95E8)\u6709\u4EBA\u5728\u804C\uFF0C\u4E0D\u80FD\u5220\u9664\uFF01";
		msgDeptExistInEmpOrgHist = "{0}\u5728\u5458\u5DE5\u7EC4\u7EC7\u7ED3\u6784\u5386\u53F2\u4E2D\u5B58\u5728\uFF0C\u4E0D\u80FD\u5220\u9664\uFF01";
		msgDeptExistInDeptHist = "{0}\u5728\u7EC4\u7EC7\u7ED3\u6784\u5386\u53F2\u4E2D\u4E3A{1}\u7684\u7236\u8282\u70B9\uFF0C\u4E0D\u80FD\u5220\u9664\uFF01";
		msgDeptExistInPBHist = "{0}\u5728\u804C\u4F4D{1}\u5386\u53F2\u4E2D\u5B58\u5728\uFF0C\u4E0D\u80FD\u5220\u9664\uFF01";
		msgDeptRelate = "\u90E8\u95E8{0}\u4E0E{1}\u6709\u5173\u8054\uFF0C\u4E0D\u80FD\u5220\u9664\uFF01";
	}

	public String operateDeleteDept(String deptId) {
		IDepartmentBO deptBo = (IDepartmentBO) SpringBeanFactory
				.getBean("departmentBO");
		IPositionBaseBo pbBo = (IPositionBaseBo) SpringBeanFactory
				.getBean("positionBaseBo");
		List allSubDeptList = deptBo.getAllSubDepts(null,
				new String[] { deptId });
		Department currentDept = null;
		int i = 0;
		do {
			if (allSubDeptList == null || i >= allSubDeptList.size())
				break;
			currentDept = (Department) allSubDeptList.get(i);
			if (deptId.equals(currentDept.getId()))
				break;
			i++;
		} while (true);
		List directSubDeptList = new ArrayList();
		for (i = 0; allSubDeptList != null && i < allSubDeptList.size(); i++) {
			Department tempDept = (Department) allSubDeptList.get(i);
			if (currentDept.getId().equals(
					tempDept.getDepartmentParentId().getId()))
				directSubDeptList.add(tempDept);
		}

		for (i = 0; i < directSubDeptList.size(); i++) {
			Department directSubDept = (Department) directSubDeptList.get(i);
			String tempInfo = operateDeleteDept(directSubDept.getId());
			if (!"SUCC".equals(tempInfo))
				return tempInfo;
		}

		List pbList = pbBo.getAllPbsOfDept(new String[] { deptId });
		for (i = 0; i < pbList.size(); i++) {
			PositionBase pb = (PositionBase) pbList.get(i);
			String tempInfo = operateDeletePB(pb);
			if (!tempInfo.equals("SUCC"))
				return tempInfo;
		}
		String tempInfo = operateDeleteCurrentDept(currentDept);
		if (!tempInfo.equals("SUCC"))
			return tempInfo;
		else
			return "SUCC";
	}

	public String operateDeletePB(PositionBase pb) {
		IDepartmentDAO dao = (IDepartmentDAO) SpringBeanFactory
				.getBean("departmentDAO");
		IPositionBo posBo = (IPositionBo) SpringBeanFactory
				.getBean("positionBo");
		String info = "SUCC";
		info = deletePBCheck(pb);
		if (!info.trim().equals("SUCC"))
			return info;
		String hql = (new StringBuilder()).append(
				"update PositionBase set pbSupId.id=null where pbSupId='")
				.append(pb.getId()).append("'").toString();
		dao.exeHql(hql);
		DetachedCriteria dc = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Position.class);
		dc.add(Restrictions.eq((new StringBuilder()).append("positionPbId.")
				.append(PositionBase.PROP_ID).toString(), pb.getId()));
		List nonRespPosList = dao.findByCriteria(dc);
		for (int i = 0; nonRespPosList != null && i < nonRespPosList.size(); i++) {
			Position pos = (Position) nonRespPosList.get(i);
			posBo.delPosition(pos.getId(), true);
		}

		hql = (new StringBuilder()).append(
				"delete from Ouresponse where ourPb.id='").append(pb.getId())
				.append("'").toString();
		dao.exeHql(hql);
		hql = (new StringBuilder()).append(
				"delete from Ouperfcriteria where oupPb.id='").append(
				pb.getId()).append("'").toString();
		dao.exeHql(hql);
		hql = (new StringBuilder()).append(
				"delete from Ouqualify where ouqPbId.id='").append(pb.getId())
				.append("'").toString();
		dao.exeHql(hql);
		operateDeleteLogsById(pb.getId());
		hql = (new StringBuilder()).append(
				"delete from PositionBaseHist where pbhiPbId.id='").append(
				pb.getId()).append("'").toString();
		dao.exeHql(hql);
		hql = (new StringBuilder()).append(
				"delete from PositionBase where id='").append(pb.getId())
				.append("'").toString();
		dao.exeHql(hql);
		return info;
	}

	public String operateDeleteCurrentDept(Department currentDept) {
		IDepartmentDAO dao = (IDepartmentDAO) SpringBeanFactory
				.getBean("departmentDAO");
		String info = "SUCC";
		info = deleteDeptCheck(currentDept);
		if (!info.trim().equals("SUCC")) {
			return info;
		} else {
			String hql = (new StringBuilder()).append(
					"delete from Ouresponse where ourDept.id='").append(
					currentDept.getId()).append("'").toString();
			dao.exeHql(hql);
			hql = (new StringBuilder()).append(
					"delete from Ouperfcriteria where oupDept.id='").append(
					currentDept.getId()).append("'").toString();
			dao.exeHql(hql);
			hql = (new StringBuilder()).append(
					"delete from Depthist where dhiDeptNo.id='").append(
					currentDept.getId()).append("'").toString();
			dao.exeHql(hql);
			hql = (new StringBuilder()).append(
					"delete from Department where id='").append(
					currentDept.getId()).append("'").toString();
			dao.exeHql(hql);
			operateDeleteLogsById(currentDept.getId());
			return info;
		}
	}

	public String operateDisableDept(String deptId) {
		IDepartmentBO deptBo = (IDepartmentBO) SpringBeanFactory
				.getBean("departmentBO");
		IPositionBaseBo pbBo = (IPositionBaseBo) SpringBeanFactory
				.getBean("positionBaseBo");
		List allSubDeptList = deptBo.getAllSubDepts(new Integer(1),
				new String[] { deptId });
		Department currentDept = null;
		int i = 0;
		do {
			if (allSubDeptList == null || i >= allSubDeptList.size())
				break;
			currentDept = (Department) allSubDeptList.get(i);
			if (deptId.equals(currentDept.getId()))
				break;
			i++;
		} while (true);
		List directSubDeptList = new ArrayList();
		for (i = 0; allSubDeptList != null && i < allSubDeptList.size(); i++) {
			Department tempDept = (Department) allSubDeptList.get(i);
			if (tempDept.getDepartmentStatus().intValue() != 0
					&& currentDept.getId().equals(
							tempDept.getDepartmentParentId().getId()))
				directSubDeptList.add(tempDept);
		}

		for (i = 0; i < directSubDeptList.size(); i++) {
			Department directSubDept = (Department) directSubDeptList.get(i);
			String tempInfo = operateDisableDept(directSubDept.getId());
			if (!"SUCC".equals(tempInfo))
				return tempInfo;
		}

		List pbList = pbBo.getActivePbsByDept(deptId);
		for (i = 0; i < pbList.size(); i++) {
			PositionBase pb = (PositionBase) pbList.get(i);
			String tempInfo = operateDisablePB(pb);
			if (!tempInfo.equals("SUCC"))
				return tempInfo;
		}

		String tempInfo = operateDisableCurrentDept(currentDept);
		if (!tempInfo.equals("SUCC"))
			return tempInfo;
		else
			return "SUCC";
	}

	public String operateDisablePB(PositionBase pb) {
		IDepartmentDAO dao = (IDepartmentDAO) SpringBeanFactory
				.getBean("departmentDAO");
		IPositionBo posBo = (IPositionBo) SpringBeanFactory
				.getBean("positionBo");
		String info = "SUCC";
		String hql = (new StringBuilder()).append(
				"select count(*) from Employee where empPbNo.id='").append(
				pb.getId()).append("' and empStatus = 1").toString();
		List list = dao.exeHqlList(hql);
		if (list != null
				&& list.get(0) != null
				&& ((Long) (Long) list.get(0)).longValue() > (new Long(0L))
						.longValue())
			info = (new StringBuilder()).append(info).append(
					StringUtil.message(msgDelPBIsUsed, new Object[] { pb
							.getPbName() })).toString();
		if (!info.equals("SUCC"))
			return info.replace("SUCC", "");
		hql = (new StringBuilder()).append(
				"update PositionBase set pbSupId.id=null where pbSupId='")
				.append(pb.getId()).append("'").toString();
		dao.exeHql(hql);
		DetachedCriteria dc = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Position.class);
		dc.createAlias(Position.PROP_POSITION_PB_ID, "positionPbId", 1);
		dc.add(Restrictions.ne((new StringBuilder()).append("positionPbId.")
				.append(PositionBase.PROP_PB_IN_CHARGE).toString(),
				new Integer(1)));
		dc.add(Restrictions.eq((new StringBuilder()).append("positionPbId.")
				.append(PositionBase.PROP_ID).toString(), pb.getId()));
		List nonRespPosList = dao.findByCriteria(dc);
		for (int i = 0; nonRespPosList != null && i < nonRespPosList.size(); i++) {
			Position pos = (Position) nonRespPosList.get(i);
			posBo.delPosition(pos.getId(), false);
		}

		pb.setPbStatus(new Integer(0));
		operateSavePBOwnAndHist(pb);
		ISysLogBO logBO = (ISysLogBO) SpringBeanFactory.getBean("logBO");
		String msg = StringUtil.message(msgDisablePB, new Object[] { pb
				.getPbName() });
		logBO.addToSyslog("positionbase", getCurrentEmp().getId(), null, pb
				.getId(), 0, "\u505C\u7528", msg);
		return "SUCC";
	}

	public String operateEnablePB(PositionBase pb) {
		String info = "SUCC";
		pb.setPbStatus(new Integer(1));
		operateSavePBOwnAndHist(pb);
		ISysLogBO logBO = (ISysLogBO) SpringBeanFactory.getBean("logBO");
		String msg = StringUtil.message(msgEnablePB, new Object[] { pb
				.getPbName() });
		logBO.addToSyslog("positionbase", getCurrentEmp().getId(), null, pb
				.getId(), 0, "\u542F\u7528", msg);
		return info;
	}

	public String operateDisableCurrentDept(Department dept) {
		IDepartmentDAO dao = (IDepartmentDAO) SpringBeanFactory
				.getBean("departmentDAO");
		IDeptHistBo deptHistBo = (IDeptHistBo) SpringBeanFactory
				.getBean("deptHistBo");
		String info = "SUCC";
		String hql = (new StringBuilder()).append(
				"select count(*) from Employee where empDeptNo.id='").append(
				dept.getId()).append("' and empStatus = 1").toString();
		List list = dao.exeHqlList(hql);
		if (list != null
				&& list.get(0) != null
				&& ((Long) (Long) list.get(0)).longValue() > (new Long(0L))
						.longValue())
			info = (new StringBuilder()).append(info).append(
					StringUtil.message(msgDepIsUsed, new Object[] { dept
							.getDepartmentName() })).toString();
		if (!info.equals("SUCC")) {
			return info.replace("SUCC", "");
		} else {
			dept.setDepartmentStatus(new Integer(0));
			operateSaveDeptOwnAndHist(dept);
			ISysLogBO logBO = (ISysLogBO) SpringBeanFactory.getBean("logBO");
			String msg = StringUtil.message(msgDisableDept, new Object[] { dept
					.getDepartmentName() });
			logBO.addToSyslog("department", getCurrentEmp().getId(), null, dept
					.getId(), 0, "\u505C\u7528", msg);
			return info;
		}
	}

	public String operateEnableCurrentDept(Department dept) {
		String info = "SUCC";
		dept.setDepartmentStatus(new Integer(1));
		operateSaveDeptOwnAndHist(dept);
		ISysLogBO logBO = (ISysLogBO) SpringBeanFactory.getBean("logBO");
		String msg = StringUtil.message(msgEnableDept, new Object[] { dept
				.getDepartmentName() });
		logBO.addToSyslog("department", getCurrentEmp().getId(), null, dept
				.getId(), 0, "\u542F\u7528", msg);
		return info;
	}

	public String operateDeptRename(Department dept) {
		IDepartmentDAO dao = (IDepartmentDAO) SpringBeanFactory
				.getBean("departmentDAO");
		IDeptHistBo deptHistBo = (IDeptHistBo) SpringBeanFactory
				.getBean("deptHistBo");
		String info = "SUCC";
		String hql = (new StringBuilder()).append(
				"select departmentName from Department where id='").append(
				dept.getId()).append("'").toString();
		List list = dao.exeHqlList(hql);
		String lastName = list == null || list.get(0) == null ? ""
				: (String) list.get(0);
		operateSaveDeptOwnAndHist(dept);
		ISysLogBO logBO = (ISysLogBO) SpringBeanFactory.getBean("logBO");
		String msg = StringUtil.message(msgRenameDept, new Object[] { lastName,
				dept.getDepartmentName() });
		logBO.addToSyslog("department", getCurrentEmp().getId(), null, dept
				.getId(), 0, "\u66F4\u540D", msg);
		return info;
	}

	public String operateDeptMove(Department fromDept, Department toDept) {
		IPositionBaseBo pbBo = (IPositionBaseBo) SpringBeanFactory
				.getBean("positionBaseBo");
		IPositionBo posBo = (IPositionBo) SpringBeanFactory
				.getBean("positionBo");
		IDepartmentDAO dao = (IDepartmentDAO) SpringBeanFactory
				.getBean("departmentDAO");
		String lastParentName = fromDept.getDepartmentParentId()
				.getDepartmentName();
		fromDept.setDepartmentParentId(toDept);
		operateSaveDeptOwnAndHist(fromDept);
		ISysLogBO logBO = (ISysLogBO) SpringBeanFactory.getBean("logBO");
		String msg = StringUtil.message(msgMoveDept, new Object[] {
				fromDept.getDepartmentName(), lastParentName,
				toDept.getDepartmentName() });
		logBO.addToSyslog("department", getCurrentEmp().getId(), null, fromDept
				.getId(), 0, "\u79FB\u52A8", msg);
		PositionBase fromRespPB = pbBo.getRespPBofDept(fromDept.getId());
		String lastPBSupName = fromRespPB.getPbSupId() == null ? ""
				: fromRespPB.getPbSupId().getPbName();
		PositionBase toRespPB = pbBo.getRespPBofDept(toDept.getId());
		if (fromRespPB != null)
			fromRespPB.setPbSupId(toRespPB);
		operateSavePBOwnAndHist(fromRespPB);
		msg = StringUtil.message(msgMovePB, new Object[] {
				fromRespPB.getPbName(), lastPBSupName, toRespPB.getPbName() });
		logBO.addToSyslog("positionbase", getCurrentEmp().getId(), null,
				fromRespPB.getId(), 0, "\u79FB\u52A8", msg);
		Position respPos = posBo.getRespPosOfDept(fromDept.getId());
		Position respPosParent = posBo.getRespPosOfDept(fromDept
				.getDepartmentParentId().getId());
		if (respPos != null)
			respPos.setPositionParentId(respPosParent);
		dao.saveOrupdate(respPos);
		return "SUCC";
	}

	public boolean operateSaveDeptOwnAndHist(Department dept) {
		IDepartmentDAO dao = (IDepartmentDAO) SpringBeanFactory
				.getBean("departmentDAO");
		IDeptHistBo deptHistBo = (IDeptHistBo) SpringBeanFactory
				.getBean("deptHistBo");
		Depthist oldDeptHist = deptHistBo.getLatestHistOfDept(dept);
		if (oldDeptHist != null)
			oldDeptHist.setDhiValidTo(new Date());
		Depthist newDeptHist = new Depthist(null, dept, dept
				.getDepartmentParentId(), dept.getDepartmentName(), dept
				.getDepartmentStatus(), new Date());
		if (dept != null)
			dao.saveOrupdate(dept);
		if (oldDeptHist != null)
			dao.saveOrupdate(oldDeptHist);
		if (newDeptHist != null)
			dao.saveOrupdate(newDeptHist);
		return true;
	}

	public boolean operateSavePBOwnAndHist(PositionBase pb) {
		IPositionBaseHistBo positionBaseHistBo = (IPositionBaseHistBo) SpringBeanFactory
				.getBean("positionBaseHistBo");
		IDepartmentDAO dao = (IDepartmentDAO) SpringBeanFactory
				.getBean("departmentDAO");
		PositionBaseHist oldPBHist = positionBaseHistBo.getLatestPBHist(pb);
		PositionBaseHist newPBHist = null;
		if (oldPBHist != null)
			oldPBHist.setPbhiValidTo(new Date());
		newPBHist = new PositionBaseHist(null, pb, pb.getPbDeptId(), pb
				.getPbStatus(), pb.getPbMaxEmployee(), pb.getPbInCharge(),
				new Date(), null);
		if (pb != null)
			dao.saveOrupdate(pb);
		if (oldPBHist != null)
			dao.saveOrupdate(oldPBHist);
		if (newPBHist != null)
			dao.saveOrupdate(newPBHist);
		return true;
	}

	public Employee getCurrentEmp() {
		HttpSession session = null;
		if (WebContextFactory.get() != null)
			session = WebContextFactory.get().getSession();
		else if (ServletActionContext.getRequest() != null)
			session = ServletActionContext.getRequest().getSession();
		Employee currentEmp = ((Userinfo) session.getAttribute("userinfo"))
				.getEmployee();
		return currentEmp;
	}

	public boolean operateDeleteLogsById(String id) {
		IDepartmentDAO dao = (IDepartmentDAO) SpringBeanFactory
				.getBean("departmentDAO");
		String hql = (new StringBuilder()).append(
				"delete from Syslog where slRecordId='").append(id).append("'")
				.toString();
		dao.exeHql(hql);
		return true;
	}

	public String deletePBCheck(PositionBase pb) {
		String info = "";
		if (checkRelate("Employee", "empPbNo.id", pb.getId()))
			info = StringUtil.addConcatStr(info, "\u3001",
					new Object[] { "\u6709\u5728\u804C\u5458\u5DE5" });// 有在职员工
		// if (checkRelate("EmpHistOrg", "ehoPbNo.id", pb.getId()))
		// info = StringUtil
		// .addConcatStr(
		// info,
		// "\u3001",
		// new Object[] { "\u5B58\u5728\u7EC4\u7EC7\u7ED3\u6784\u5386\u53F2"
		// });// 存在组织结构历史
		if (checkRelate("Emptransfer", "eftOldPbId.id", pb.getId())
				|| checkRelate("Emptransfer", "eftNewPbId.id", pb.getId()))
			info = StringUtil
					.addConcatStr(
							info,
							"\u3001",
							new Object[] { "\u4E0E\u5458\u5DE5\u53D8\u52A8\u8BB0\u5F55\u5173\u8054" });// 与员工变动记录关联
		if (checkRelate("Empeval", "eePbNo.id", pb.getId()))
			info = StringUtil
					.addConcatStr(
							info,
							"\u3001",
							new Object[] { "\u4E0E\u8003\u8BC4\u8BB0\u5F55\u5173\u8054" });// 与考评记录关联
		if (checkRelate("Empreward", "erPbNo.id", pb.getId()))
			info = StringUtil
					.addConcatStr(
							info,
							"\u3001",
							new Object[] { "\u4E0E\u5956\u60E9\u8BB0\u5F55\u5173\u8054" });
		if (checkRelate("Empquit", "eqPbNo.id", pb.getId()))
			info = StringUtil
					.addConcatStr(
							info,
							"\u3001",
							new Object[] { "\u4E0E\u79BB\u804C\u8BB0\u5F55\u5173\u8054" });
		if (checkRelate("Empsalarypay", "espPbNo.id", pb.getId()))
			info = StringUtil
					.addConcatStr(
							info,
							"\u3001",
							new Object[] { "\u4E0E\u85AA\u8D44\u53D1\u653E\u8BB0\u5F55\u5173\u8054" });
		if (checkRelate("Attendmonthly", "attmPbNo.id", pb.getId()))
			info = StringUtil
					.addConcatStr(
							info,
							"\u3001",
							new Object[] { "\u4E0E\u6BCF\u6708\u8003\u52E4\u8BB0\u5F55\u5173\u8054" });
		if (checkRelate("Recruitplan", "recpPbNo.id", pb.getId()))
			info = StringUtil
					.addConcatStr(
							info,
							"\u3001",
							new Object[] { "\u4E0E\u62DB\u8058\u8BA1\u5212\u5173\u8054" });
		if (StringUtils.isNotEmpty(info))
			return (new StringBuilder()).append(pb.getPbName()).append(
					"\u4E0D\u80FD\u5220\u9664\uFF01").append(info).toString();
		else
			return "SUCC";
	}

	public String deleteDeptCheck(Department dept) {
		IDepartmentDAO dao = (IDepartmentDAO) SpringBeanFactory
				.getBean("departmentDAO");
		String info = "";
		if (checkRelate("Employee", "empDeptNo.id", dept.getId()))
			info = StringUtil.addConcatStr(info, "\u3001",
					new Object[] { "\u6709\u5728\u804C\u5458\u5DE5" });// 有在职员工
		if (checkRelate("EmpHistOrg", "ehoDeptNo.id", dept.getId()))
			info = StringUtil
					.addConcatStr(
							info,
							"\u3001",
							new Object[] { "\u5B58\u5728\u7EC4\u7EC7\u7ED3\u6784\u5386\u53F2" });// 存在组织结构历史
		String hql = (new StringBuilder())
				.append(
						"select distinct(hist.dhiDeptName) from Depthist hist left join hist.dhiDeptSupId deptSup where deptSup.id='")
				.append(dept.getId()).append("'").toString();
		List list = dao.exeHqlList(hql);
		if (list.size() > 0 && list != null)
			info = StringUtil.addConcatStr((new StringBuilder()).append(info)
					.append("\u3001\u6709\u5B50\u90E8\u95E8\u5386\u53F2\uFF1A")// 、
					// 有子部门历史
					// ：
					.toString(), "/", list.toArray(new Object[list.size()]));
		hql = (new StringBuilder())
				.append(
						"select distinct(pb.pbName) from PositionBaseHist hist left join hist.pbhiPbId pb where hist.pbhiDeptId.id='")
				.append(dept.getId()).append("'").toString();
		list = dao.exeHqlList(hql);
		if (list.size() > 0 && list != null)
			info = StringUtil.addConcatStr((new StringBuilder()).append(info)
					.append("\u3001\u6709\u804C\u4F4D\u5386\u53F2\uFF1A")//、有职位历史
					// ：
					.toString(), "/", list.toArray(new Object[list.size()]));
		if (checkRelate("Emptransfer", "eftOldDeptNo.id", dept.getId())
				|| checkRelate("Emptransfer", "eftNewDeptNo.id", dept.getId()))
			info = (new StringBuilder())
					.append(info)
					.append(
							StringUtil
									.addConcatStr(
											info,
											"\u3001",
											new Object[] { "\u4E0E\u5458\u5DE5\u53D8\u52A8\u8BB0\u5F55\u5173\u8054" }))
					.toString();
		if (checkRelate("Empreward", "department.id", dept.getId()))
			info = StringUtil
					.addConcatStr(
							info,
							"\u3001",
							new Object[] { "\u4E0E\u5956\u60E9\u8BB0\u5F55\u5173\u8054" });
		if (checkRelate("Empeval", "department.id", dept.getId()))
			info = StringUtil
					.addConcatStr(
							info,
							"\u3001",
							new Object[] { "\u4E0E\u8003\u8BC4\u8BB0\u5F55\u5173\u8054" });
		if (checkRelate("Empquit", "eqDeptNo.id", dept.getId()))
			info = StringUtil
					.addConcatStr(
							info,
							"\u3001",
							new Object[] { "\u4E0E\u79BB\u804C\u8BB0\u5F55\u5173\u8054" });
		if (checkRelate("Empsalarypay", "espDept.id", dept.getId()))
			info = StringUtil
					.addConcatStr(
							info,
							"\u3001",
							new Object[] { "\u4E0E\u85AA\u8D44\u53D1\u653E\u8BB0\u5F55\u5173\u8054" });
		if (checkRelate("Attendmonthly", "attmDept.id", dept.getId()))
			info = StringUtil
					.addConcatStr(
							info,
							"\u3001",
							new Object[] { "\u4E0E\u6BCF\u6708\u8003\u52E4\u8BB0\u5F55\u5173\u8054" });
		if (checkRelate("Attendshiftareadept", "asadDeptId.id", dept.getId()))
			info = StringUtil
					.addConcatStr(
							info,
							"\u3001",
							new Object[] { "\u4E0E\u73ED\u6B21\u8BBE\u7F6E\u5173\u8054" });
		if (checkRelate("Tremployeeplan", "trpTraineeDept.id", dept.getId()))
			info = StringUtil
					.addConcatStr(
							info,
							"\u3001",
							new Object[] { "\u4E0E\u57F9\u8BAD\u8BA1\u5212\u5173\u8054" });
		if (checkRelate("Recruitplan", "recpDepartmentNo.id", dept.getId()))
			info = StringUtil
					.addConcatStr(
							info,
							"\u3001",
							new Object[] { "\u4E0E\u62DB\u8058\u8BA1\u5212\u5173\u8054" });
		if (checkRelate("AuthorityPos", "apDept.id", dept.getId()))
			info = StringUtil
					.addConcatStr(
							info,
							"\u3001",
							new Object[] { "\u4E0E\u5BA1\u6279\u8BBE\u7F6E\u5173\u8054" });
		if (StringUtils.isNotEmpty(info))
			return (new StringBuilder()).append(dept.getDepartmentName())
					.append("\u4E0D\u80FD\u5220\u9664\uFF01").append(info)
					.toString();
		else
			return "SUCC";
	}

	private boolean checkRelate(String table, String field, String id) {
		IDepartmentDAO dao = (IDepartmentDAO) SpringBeanFactory
				.getBean("departmentDAO");
		String hql = (new StringBuilder()).append("select count(*) from ")
				.append(table).append(" where ").append(field).append("='")
				.append(id).append("'").toString();
		List list = dao.exeHqlList(hql);
		return list != null && list.get(0) != null
				&& ((Long) (Long) list.get(0)).longValue() > 0L;
	}

	String msgDisableDept;
	String msgEnablePB;
	String msgEnableDept;
	String msgRenameDept;
	String msgMoveDept;
	String msgMovePB;
	String msgMovePBDept;
	String msgDepIsUsed;
	String msgPBExceed;
	String msgDisablePBRelate;
	String msgDisablePB;
	String msgDelPBExistInEmpOrgHist;
	String msgDelPBIsUsed;
	String msgDeletePBIsUsed;
	String msgPBRelate;
	String msgDeleteDepIsUsed;
	String msgDeptExistInEmpOrgHist;
	String msgDeptExistInDeptHist;
	String msgDeptExistInPBHist;
	String msgDeptRelate;
}
